package gold.digger;

import gold.utils.InputUtil;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC798 {
    public long startExecuteTime = System.currentTimeMillis();


    /*
     * @param 此题目参考了别人代码
     * 这是因为问题情况较为复杂
     * 未来需要再次复习此道题目
     * 确定每个元素移动空间上下界，很注重分析的题目
     * @return:
     */
    class Solution {
        public int bestRotation(int[] nums) {
            int n = nums.length;
            int[] moves = new int[n + 1];
            for (int step = 0; step < n; step++) {
                if (nums[step] > n - 1)
                    continue;
                if (nums[step] >= step) {
                    moves[(step + 1) % n]++;
                    moves[(n - nums[step] + step + 1) % n]--;
                } else {
                    moves[0]++;
                    moves[(step - nums[step] + 1) % n]--;
                    moves[(step + 1) % n]++;
                    moves[n]--;
                }
            }

            int sum = 0;
            int maxSum = 0;
            int ans = 0;
            for (int i = 0; i < n; i++) {
                sum += moves[i];
                if (sum > maxSum) {
                    maxSum = sum;
                    ans = i;
                }
            }
            return ans;
        }
    }

    public void run() {
        int[] arr = InputUtil.toIntegerArray("[1,2,3]");
        System.out.println(new Solution().toString());
    }

    public static void main(String[] args) throws Exception {
        LC798 an = new LC798();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
